package com.ehualu.getes.until;

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author: 吴敬超
 * @Date: 2019/11/15 9:11
 */
public class ESsdUntil {


    public List<String[]> getgcjl(String starttime, String endTimeStr, String index, String[] kkid, RestHighLevelClient client) {

        List<String[]> list = new ArrayList<>();

        List<String> flaglist = new ArrayList<>();

        String indexName = index;

        try {

            SearchRequest searchRequest = new SearchRequest(indexName);
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//			sourceBuilder.from(0); // 默认0
            sourceBuilder.size(10000);// 默认10条

            QueryBuilder queryBuilder;
            if (kkid[1]==null){

                 queryBuilder = QueryBuilders.boolQuery()
                        .must(QueryBuilders.termQuery("caddresscode.keyword", kkid[0]))
                        .must(QueryBuilders.rangeQuery("createtime").from(starttime).to(endTimeStr));
            }else {


                 queryBuilder = QueryBuilders.boolQuery()
                        .must(QueryBuilders.termsQuery("caddresscode.keyword", kkid))
                        .must(QueryBuilders.rangeQuery("createtime").from(starttime).to(endTimeStr));

            }




//            根据时间升序排序
            sourceBuilder.sort("createtime", SortOrder.ASC);
            sourceBuilder.query(queryBuilder);
            searchRequest.source(sourceBuilder);
            SearchResponse response = client.search(searchRequest);

            Long rtlh=  response.getHits().getTotalHits();
            System.out.println("查询数据量："+rtlh);
            SearchHits hits = response.getHits();
            SearchHit[] searchHits = hits.getHits();
            int sl = searchHits.length;
            System.out.println("查询显示数据量：" + sl);
            for (SearchHit hit : searchHits) {

                String gcinfo[] = new String[4];

                Map<String, Object> sourceAsMap = hit.getSourceAsMap();// 获取查询结果json

                String strccarnumber = (String) sourceAsMap.get("ccarnumber");

                String gcsj = (String) sourceAsMap.get("createtime");
                String jd = (String) sourceAsMap.get("jd");
                String wd = (String) sourceAsMap.get("wd");



//                System.out.println("ccarnumber:" + strccarnumber);
//                System.out.println("createtime:" + gcsj);
                gcinfo[0] = strccarnumber;
                gcinfo[1] = gcsj;
                gcinfo[2] = jd;
                gcinfo[3] = wd;


                if (flaglist.contains(strccarnumber)) {

                    System.out.println("该车牌已经记录过了");
                } else {

//                    System.out.println("该车牌尚未记录");

                    flaglist.add(strccarnumber);

                    list.add(gcinfo);
                }

            }
            System.out.println("循环*****结束");

        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e1) {
            e1.printStackTrace();
        }

        return list;
    }

}
